package com.origin.app.utils

import android.graphics.BitmapFactory
import android.util.Log
import com.amap.api.maps.AMap
import com.amap.api.maps.CameraUpdateFactory
import com.amap.api.maps.model.BitmapDescriptorFactory
import com.amap.api.maps.model.LatLng
import com.amap.api.maps.model.LatLngBounds
import com.amap.api.maps.model.Marker
import com.amap.api.maps.model.MarkerOptions
import com.origin.app.MyApplication
import com.origin.app.R
import com.origin.app.bean.CompanyPointBean
import com.origin.uilibrary.xbanner.XBannerUtils
import com.origin.utils.log.LogUtils


object MapUtils {


    private val normalMarkerMap = mutableListOf<Marker>()
    private val exceptionMarkerMap = mutableListOf<Marker>()
    private val offlineMarkerMap = mutableListOf<Marker>()
    private val outLineMarkerMap = mutableListOf<Marker>()

    private var _normalMarkerOption: MarkerOptions? = null
    private var _exceptionMarkerOption: MarkerOptions? = null
    private var _offlineMarkerOption: MarkerOptions? = null


    private var _normalCardMarkerOption: MarkerOptions? = null
    private var _exceptionCardMarkerOption: MarkerOptions? = null
    private var _offlineCardMarkerOption: MarkerOptions? = null

    fun clearMarkerMap() {
        normalMarkerMap.clear();
    }

    private val markerMapByIdPointBean = mutableMapOf<String, Marker>()

    fun getCompanyPointBeanByCode(code: String): Marker? {
        return markerMapByIdPointBean[code]
    }

    private fun findMarkerMapByState(state: Int): MutableList<Marker> {
        if (state == 1) {
            return normalMarkerMap
        }
        if (state == 2) {
            return exceptionMarkerMap
        }
        if (state == 3) {
            return offlineMarkerMap
        }
        if (state == 4) {
            return outLineMarkerMap
        }
        throw Exception("未找到对应的状态")
    }

    private fun moveListMarker(beforeState: Int, afterState: Int, marker: Marker) {
        var list1 = findMarkerMapByState(beforeState);
        var list2 = findMarkerMapByState(afterState);
        val index = list1.indexOf(marker);
        if (index == -1) {
            return;
        }
        list2.add(list1.removeAt(index))
    }

    fun switchEquipmentStateMarkerMap(beforeState: Int, afterState: Int, marker: Marker) {
        moveListMarker(beforeState, afterState, marker)
    }

    fun classificationMarker(state: Int, marker: Marker) {

        val bean = marker.`object`;

        if (bean is CompanyPointBean) {
            markerMapByIdPointBean[bean.in_Code] = marker
        }
        if (state == 1) {
            normalMarkerMap.add(marker)
        } else if (state == 2) {
            exceptionMarkerMap.add(marker)
        } else if (state == 3) {
            offlineMarkerMap.add(marker)
        }

    }


    private fun moveOtherMarker(state: Int, isShow: Boolean) {
        val list = findMarkerMapByState(state)
        // 临时保存
        val tmpList = mutableListOf<Marker>()
        tmpList.addAll(list)

        for ((index, bean) in list.withIndex()) {
            bean.isVisible = isShow
            val companyPointBean = bean.`object` as CompanyPointBean
            if (companyPointBean.equipmentState != state) {
                bean.isVisible = false
                tmpList.remove(bean)
                val listAdd = findMarkerMapByState(companyPointBean.equipmentState)
                listAdd.add(bean)
            }
        }
        list.clear()
        list.addAll(tmpList)
    }

    fun showMarker(tag: Int) {
        when (tag) {
            1 -> {
                moveOtherMarker(1, true)
                exceptionMarkerMap.forEach {
                    it.isVisible = false
                }
                offlineMarkerMap.forEach {
                    it.isVisible = false
                }
            }

            2 -> {
                moveOtherMarker(2, true)
                normalMarkerMap.forEach {
                    it.isVisible = false
                }

                offlineMarkerMap.forEach {
                    it.isVisible = false
                }
            }

            3 -> {
                moveOtherMarker(3, true)
                normalMarkerMap.forEach {
                    it.isVisible = false
                }
                exceptionMarkerMap.forEach {
                    it.isVisible = false
                }

            }
        }

    }

    fun showAllMarker(tag: Int) {
        normalMarkerMap.forEach {
            it.isVisible = true
        }
        exceptionMarkerMap.forEach {
            it.isVisible = true
        }
        offlineMarkerMap.forEach {
            it.isVisible = true
        }
    }

    fun getMarker(state: Int, isCar: Boolean): MarkerOptions {
        if (state == 1) {
            if (isCar && _normalCardMarkerOption != null) {
                return _normalCardMarkerOption!!
            }
            if (!isCar && _normalMarkerOption != null) {
                return _normalMarkerOption!!
            }
        } else if (state == 2) {
            if (isCar && _exceptionCardMarkerOption != null) {
                return _exceptionCardMarkerOption!!
            }
            if (!isCar && _exceptionMarkerOption != null) {
                return _exceptionMarkerOption!!
            }
        } else if (state == 3) {
            if (isCar && _offlineCardMarkerOption != null) {
                return _offlineCardMarkerOption!!
            }
            if (!isCar && _offlineMarkerOption != null) {
                return _offlineMarkerOption!!
            }
        }

        val markerOption = MarkerOptions()

        markerOption.draggable(false) //设置Marker可拖动

        markerOption.icon(
            AppUtils.getPointInfoAvatar(state, isCar)
        )

        // 将Marker设置为贴地显示，可以双指下拉地图查看效果
        markerOption.setFlat(true) //设置marker平贴地图效果
        if (state == 1) {
            if (isCar) {
                _normalCardMarkerOption = markerOption;
            } else {
                _normalMarkerOption = markerOption;
            }

        } else if (state == 2) {
            if (isCar) {
                _exceptionCardMarkerOption = markerOption;
            } else {
                _exceptionMarkerOption = markerOption;
            }

        } else if (state == 3) {
            if (isCar) {
                _offlineCardMarkerOption = markerOption;
            } else {
                _offlineMarkerOption = markerOption;
            }
        }

        return markerOption
    }


    fun fitMarkersToBounds(markers: List<Marker>, aMap: AMap) {
        if (markers.isEmpty()) {
            return
        }

        // 创建 LatLngBounds.Builder
        val builder = LatLngBounds.Builder()

        // 遍历所有 Marker，扩展 LatLngBounds
        for (marker in markers) {
            builder.include(marker.position)
        }

        fitMarkersToBounds(builder,aMap)
    }

    fun fitMarkersToBoundsByLatLng(latLngList: List<LatLng>, aMap: AMap) {
        if (latLngList.isEmpty()) {
            return
        }

        // 创建 LatLngBounds.Builder
        val builder = LatLngBounds.Builder()

        // 遍历所有 Marker，扩展 LatLngBounds
        for (latLng in latLngList) {
            builder.include(latLng)
        }

        fitMarkersToBounds(builder,aMap)
    }

    private fun  fitMarkersToBounds(builder:LatLngBounds.Builder,aMap: AMap){

        // 获取完整的 LatLngBounds
        val bounds = builder.build()

        // 计算边界并更新相机位置
        val padding = XBannerUtils.dp2px(MyApplication.getInstance(), 100f) // 边距，可以根据需要调整
        val cu = CameraUpdateFactory.newLatLngBounds(bounds, padding)
        aMap.animateCamera(cu)
    }


}